package javaes.dados.generico;

import javaes.util.HibernateUtil;

import org.hibernate.Session;
import org.hibernate.Transaction;

public class InsertUpdateDeleteHibernate 
{
	 	private static Session session; 
	    private static Transaction transacao;
	    
	    public static void salva(Object obj) throws Exception
	    {
	         try
	        {
	            session                                     = HibernateUtil.getSessionFactory().getCurrentSession();
	            transacao                                   = session.beginTransaction();
	            session.saveOrUpdate(obj);
	            transacao.commit();
	        }
	        catch(Exception erro)
	        {
	            throw new Exception(erro.getMessage());
	        }
	        finally
	        {
	            try
	            {
	                if(session!= null && session.isOpen())
	                    session.close();
	            }
	            catch(Exception e)
	            {
	                throw new Exception(e.getMessage());
	            }
	        }
	    }
	    
	    public static void atualizar(Object obj) throws Exception
	    {
	         try
	        {
	            session                                     = HibernateUtil.getSessionFactory().getCurrentSession();
	            transacao                                   = session.beginTransaction();
	            session.update(obj);
	            transacao.commit();
	        }
	        catch(Exception erro)
	        {
	            throw new Exception(erro.getMessage());
	        }
	        finally
	        {
	            try
	            {
	                if(session.isOpen())
	                    session.close();
	            }
	            catch(Exception e)
	            {
	                throw new Exception(e.getMessage());
	            }
	        }
	    }
	    
	    public static void exclui(Object obj) throws Exception
	    {
	         try
	        {
	            session                                     = HibernateUtil.getSessionFactory().getCurrentSession();
	            transacao                                   = session.beginTransaction();
	            session.delete(obj);
	            transacao.commit();
	        }
	        catch(Exception erro)
	        {
	            throw new Exception(erro.getMessage());
	        }
	        finally
	        {
	            try
	            {
	                if(session.isOpen())
	                    session.close();
	            }
	            catch(Exception e)
	            {
	                throw new Exception(e.getMessage());
	            }
	        }
	    }
}
